package com.xiaobingby.easyuisys.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.xiaobingby.common.utils.ExcelUtil;
import com.xiaobingby.easyuisys.entity.PermTreegrid;
import com.xiaobingby.easyuisys.entity.PermissionExcel;
import com.xiaobingby.easyuisys.service.IPermissionService;
import com.xiaobingby.sys.entity.Permission;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 李磊
 * @date 2019/5/9
 */
@Controller("easyuiPermissionController")
@RequestMapping("/easyui/permission")
public class PermissionController {

    @Autowired
    IPermissionService permissionService;

    @GetMapping("/list")
    public String index(){
        return "/easyui/permission/permissionManager";
    }

    @GetMapping("/permTreegrid")
    @ResponseBody
    public List<PermTreegrid> permTreegrid(){
        return permissionService.getPermTreegridList();
    }

    @GetMapping("/parentMenu")
    @ResponseBody
    public List<Permission> getParentMenu(int type){
        EntityWrapper<Permission> ew = new EntityWrapper<>();
        ew.eq("type",type-1);
        return permissionService.selectList(ew);
    }

    @GetMapping("/add")
    public String add(){
        return "/easyui/permission/permissionAdd";
    }

    @GetMapping("/modify")
    public String modify(long id, Model model){
        Permission permission = permissionService.selectById(id);
        model.addAttribute("permission",permission);
        return "/easyui/permission/permissionModify";
    }

    @GetMapping("export")
    public void export(HttpServletResponse response) throws IOException {
        List<Permission> permissions = permissionService.selectList(null);
        List<PermissionExcel> permissionExcels = new ArrayList<>(permissions.size());
        for (Permission permission:permissions){
            PermissionExcel permissionExcel = new PermissionExcel();
            BeanUtils.copyProperties(permission,permissionExcel);
            permissionExcels.add(permissionExcel);
        }
        ExcelUtil.exporExcel(response,permissionExcels,"permissions");
    }
}
